<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="题目名称" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入题目名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="题型" prop="type">
        <el-select v-model="queryParams.type" placeholder="题型" size="small">
          <el-option
            v-for="dict in dict.type.edu_subject_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
         <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['edu:subject:add']">新增</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5"></el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="postList">
      <el-table-column label="题目名称" align="center" prop="title">
        <template slot-scope="scope">
          <div v-html="format(scope.row.title)"></div>
        </template>
      </el-table-column>
      <el-table-column label="题型" align="center" prop="type">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.edu_subject_type" :value="scope.row.type"/>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.ctime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['edu:subject:edit']"
          >修改</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.page"
      :limit.sync="queryParams.limit"
      @pagination="getList"
    />

    <!-- 添加或修改对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="选择题型" prop="type">
          <el-select v-model="form.type" placeholder="题型" size="small">
            <el-option
              v-for="dict in dict.type.edu_subject_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submit">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog title="" :visible.sync="openEdit"  width="1000px" :close-on-click-modal="false" append-to-body>
        <Choice v-if="form.type == 0" :form="form" @submit="handleSubmit" @cancel="handleCancel"/>
        <Judge v-if="form.type == 1" :form="form" @submit="handleSubmit" @cancel="handleCancel"/>
        <Fill v-if="form.type == 2" :form="form" @submit="handleSubmit" @cancel="handleCancel"/>
        <Answer v-if="form.type == 3" :form="form" @submit="handleSubmit" @cancel="handleCancel"/>
        <Program v-if="form.type == 4" :form="form" @submit="handleSubmit" @cancel="handleCancel"/>
    </el-dialog>
  </div>
</template>

<script>
import { listSubject, getSubject, addSubject, updateSubject, delSubject } from "@/api/edu/subject";
import Choice from './components/choice'
import Judge from './components/judge'
import Fill from './components/fill'
import Answer from './components/answer'
import Program from './components/program'
export default {
  name: "Subject",
  dicts: ['edu_subject_type'],
  components: {
    Choice,
    Judge,
    Fill,
    Answer,
    Program
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 岗位表格数据
      postList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      openEdit: true,
      // 查询参数
      queryParams: {
        page: 1,
        limit: 10,
        type: undefined,
        title: undefined
      },
      // 表单参数
      form: {
        id: undefined,
        courseId: 1,
        title: undefined,
        type: '4',
        content: undefined,
        score: undefined,
        analysis: undefined
      },
      // 表单校验
      rules: {
        name: [
          { required: true, message: "名称不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.queryParams.courseId = this.$route.query.courseId;
    this.form.courseId = this.$route.query.courseId;
    this.getList();
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true;
      listSubject(this.queryParams).then(response => {
        this.postList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    format(value) {
      return value ? value.replace(/\[\[.*\]\]/, "____") : "";
    },
    submit(){
      this.open = false;
      this.openEdit = true
      let type = this.form.type;
      this.reset()
      this.form.type = type;
    },
    // 取消按钮
    cancel() {
      this.open = false;
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        courseId: this.$route.query.courseId,
        title: undefined,
        type: '0',
        content: undefined,
        score: undefined,
        analysis: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.page = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.postId)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加题目";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      getSubject(row.id).then(response => {
        this.form = response.data;
        this.openEdit = true;
        this.title = "修改题目";
      });
    },
    handleSubmit(form){
      if (form.id != undefined) {
          updateSubject(form).then(response => {
            this.msgSuccess("修改成功");
            this.openEdit = false;
            this.getList();
          });
        } else {
          addSubject(form).then(response => {
            this.msgSuccess("新增成功");
            this.openEdit = false;
            this.getList();
          });
        }
    },
    handleCancel(){
      this.openEdit = false;
      this.reset();
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$confirm('是否确认删除?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delSubject(row.id);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    }
    
  }
};
</script>
<style scoped>

</style>
